package cn.stylefeng.guns.modular.business.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.FigureClassify;
import cn.stylefeng.guns.modular.business.enums.FigureClassifyExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.FigureClassifyMapper;
import cn.stylefeng.guns.modular.business.pojo.FigureClassifyRequest;
import cn.stylefeng.guns.modular.business.service.FigureClassifyService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 人物分类表业务实现层
 *
 * @author zhangyaowen
 * @date 2021/10/25 16:46
 */
@Service
public class FigureClassifyServiceImpl extends ServiceImpl<FigureClassifyMapper, FigureClassify> implements FigureClassifyService {

	@Override
    public void add(FigureClassifyRequest figureClassifyRequest) {
        FigureClassify figureClassify = new FigureClassify();
        BeanUtil.copyProperties(figureClassifyRequest, figureClassify);
        this.save(figureClassify);
    }

    @Override
    public void del(FigureClassifyRequest figureClassifyRequest) {
        FigureClassify figureClassify = this.queryFigureClassify(figureClassifyRequest);
        this.removeById(figureClassify.getId());
    }

    @Override
    public void edit(FigureClassifyRequest figureClassifyRequest) {
        FigureClassify figureClassify = this.queryFigureClassify(figureClassifyRequest);
        BeanUtil.copyProperties(figureClassifyRequest, figureClassify);
        this.updateById(figureClassify);
    }

    @Override
    public FigureClassify detail(FigureClassifyRequest figureClassifyRequest) {
        return this.queryFigureClassify(figureClassifyRequest);
    }

    @Override
    public PageResult<FigureClassify> findPage(FigureClassifyRequest figureClassifyRequest) {
        LambdaQueryWrapper<FigureClassify> wrapper = createWrapper(figureClassifyRequest);
        Page<FigureClassify> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Map<String,Object>> findChoose() {
        LambdaQueryWrapper<FigureClassify> queryWrapper = new LambdaQueryWrapper<>();
        List<Map<String,Object>> mapList = new ArrayList<>();
        List<Map<String,Object>> findList = new ArrayList<>();
        Map<String, Object> findYear = new HashMap<String, Object>();
        List<FigureClassify> list= this.list(queryWrapper);

//        // 根据RiverName去重
//        //1.提取出list对象中的一个属性
//        List<String> riverList1 = list.stream().map(FigureClassify::getYearClassify).collect(Collectors.toList());
//
//        //2.提取出list对象中的一个属性并去重
//        List<String> riverList2 = list.stream().map(FigureClassify::getYearClassify).distinct().collect(Collectors.toList());
//        for(String ss : riverList2){
//            Map<String, Object> map = new HashMap<String, Object>();
//            if("".equals(ss) || ss == null){
//                continue;
//            }else{
//                map.put("year",ss);
//                mapList.add(map);
//            }
//
//        }

        for(FigureClassify figureClassify : list){

            Map<String, Object> map = new HashMap<String, Object>();
            Map<String, Object> map1 = new HashMap<String, Object>();
            if(!figureClassify.getYearClassify().equals("")){
                map.put("year",figureClassify.getYearClassify());
                map.put("yearCode",figureClassify.getYearCode());
                mapList.add(map);
            }
        }
        //List<map>去重 生成新的list
        ArrayList<Map<String, Object>> rive = mapList.stream()
                .collect(Collectors
                        .collectingAndThen(Collectors
                                .toCollection(() -> new TreeSet<>(new Comparator<Map<String, Object>>() {
                                    @Override    //重写比较器
                                    public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                                        if (o1.get("year").equals(o2.get("year"))) {
                                            return 0;
                                        }
                                        return 1;
                                    }

                                })), ArrayList::new));
        findYear.put("list",rive);
        findYear.put("title","时期");
        findList.add(findYear);
        return findList;
    }

    @Override
    public List<FigureClassify> findList(FigureClassifyRequest figureClassifyRequest) {
        LambdaQueryWrapper<FigureClassify> wrapper = this.createWrapper(figureClassifyRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhangyaowen
     * @date 2021/10/25 16:46
     */
    private FigureClassify queryFigureClassify(FigureClassifyRequest figureClassifyRequest) {
        FigureClassify figureClassify = this.getById(figureClassifyRequest.getId());
        if (ObjectUtil.isEmpty(figureClassify)) {
            throw new ServiceException(FigureClassifyExceptionEnum.FIGURECLASSIFY_NOT_EXISTED);
        }
        return figureClassify;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhangyaowen
     * @date 2021/10/25 16:46
     */
    private LambdaQueryWrapper<FigureClassify> createWrapper(FigureClassifyRequest figureClassifyRequest) {
        LambdaQueryWrapper<FigureClassify> queryWrapper = new LambdaQueryWrapper<>();

        Long id = figureClassifyRequest.getId();
        String yearClassify = figureClassifyRequest.getYearClassify();
        String yearCode = figureClassifyRequest.getYearCode();
        String figureClassify = figureClassifyRequest.getFigureClassify();
        String figureCode = figureClassifyRequest.getFigureCode();

        queryWrapper.eq(ObjectUtil.isNotNull(id), FigureClassify::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(yearClassify), FigureClassify::getYearClassify, yearClassify);
        queryWrapper.like(ObjectUtil.isNotEmpty(yearCode), FigureClassify::getYearCode, yearCode);
        queryWrapper.like(ObjectUtil.isNotEmpty(figureClassify), FigureClassify::getFigureClassify, figureClassify);
        queryWrapper.like(ObjectUtil.isNotEmpty(figureCode), FigureClassify::getFigureCode, figureCode);

        return queryWrapper;
    }

}